package com.wjm.blog.core.redis;

import com.alibaba.fastjson.JSON;
import com.wjm.blog.common.base.SysUserDetailVo;
import com.wjm.blog.common.constant.CommonConst;
import com.wjm.blog.common.enums.LanguageEnum;
import com.wjm.blog.common.util.ServletUtils;
import com.wjm.blog.common.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: HttpServletRequest工具类
 * @Author: Ailos
 * @Date: 2020/4/20 14:10
 * @Version: 1.0
 */
@Slf4j
public class RequestUtil {
    private RequestUtil() {
        throw new IllegalStateException("RequestUtil class");
    }

    /**
     * 获取request
     */
    public static HttpServletRequest getRequest() {
        HttpServletRequest httpServletRequest = null;
        if (getRequestAttributes() != null) {
            httpServletRequest = getRequestAttributes().getRequest();
        }
        return httpServletRequest;
    }


    /**
     * 获取当前用户ID
     */
    public static Long getCurrentUserId() {
//        Long currentUserId = -100L;
        Long currentUserId = null;

        SysUserDetailVo userDetailVo = getUserDetailVo();
        if (userDetailVo != null) {
            currentUserId = userDetailVo.getId();
        }

        return currentUserId;
    }

    /**
     * 获取当前租户ID
     */
    public static Long getCurrentSiteid() {
//        Long siteid = -100L;
        Long siteid = null;

        SysUserDetailVo userDetailVo = getUserDetailVo();
        if (userDetailVo != null) {
            siteid = userDetailVo.getTenantId();
        }

        return siteid;
    }

    /**
     * 获取当前用户名称
     */
    public static String getCurrentUserName() {
        String currentUserName = "";

        SysUserDetailVo userDetailVo = getUserDetailVo();
        if (userDetailVo != null) {
            currentUserName = userDetailVo.getUserAccount();
        }

        return currentUserName;
    }


    public static SysUserDetailVo getUserDetailVo() {
        SysUserDetailVo userDetailVo = (SysUserDetailVo) ServletUtils.getAttribute("userDetailVo");

        if (userDetailVo == null) {
            Object initUser = RedisUtils.get("initUser");
            userDetailVo = JSON.parseObject(initUser + "", SysUserDetailVo.class);
        }
        return userDetailVo;
    }


    /**
     * 设置当前用户ID
     */
    public static void setCurrentUserId(long userId) {
        HttpServletRequest request = getRequest();
        request.setAttribute(CommonConst.CURRENT_USER_ID_KEY, userId);
    }

    /**
     * 获取response
     */
    public static HttpServletResponse getResponse() {
        return getRequestAttributes().getResponse();
    }

    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }

    /**
     * 从请求头中获取当前请求设置语言
     *
     * @return 返回语言LanguageEnum，默认使用中文
     */
    public static LanguageEnum getLanguageEnum() {
        LanguageEnum lang = LanguageEnum.CHINESE;
        HttpServletRequest request = getRequest();
        if (request != null) {
            String l = request.getHeader(CommonConst.LANGUAGE_PARAM_KEY);
            if (StringUtil.isEmpty(l)) {
                l = request.getParameter(CommonConst.LANGUAGE_PARAM_KEY);
            }
            lang = LanguageEnum.getByMessage(l);
        }
        return lang;
    }
}
